package com.opgea.crm.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.constant.ApplicationConstant.SalesCalenderType;
import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.common.vo.SalesCalendarVo;
import com.opgea.crm.domain.entities.Activity;
import com.opgea.crm.domain.entities.Employee;
import com.opgea.crm.domain.entities.Issue;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.entities.Task;
import com.opgea.crm.repository.CalenderRepo;

@Service
public class CalenderServiceImpl implements CalenderService {

	@Autowired
	private CalenderRepo calenderRepo;
	@Override
	public List<SalesCalendarVo> showCalenderGrid(Long assignToId, Date pfrom,
			Date pto, SalesCalenderType type) {
		List<SalesCalendarVo> list=new ArrayList<SalesCalendarVo>();
		  if(type.equals(ApplicationConstant.SalesCalenderType.Activity))
		  {
			  List<Activity> activities=this.calenderRepo.showCalenderGrid(assignToId, pfrom, pto);
			  for(Activity activity:activities)
			  {
				  SalesCalendarVo vo=new SalesCalendarVo();
				  vo.setDueDateString(DateUtil.dateToString(activity.getScheduledDate()));
				  vo.setName(activity.getNote());
				  if(activity.getLead()!=null){
				  vo.setCompany(activity.getLead().getCompany().getName());
				  vo.setCompanyId(activity.getLead().getCompany().getId());
				  }
				  if(activity.getAssignTo()!=null)
				  {
					  vo.setAssignTo(activity.getAssignTo().getFirstName());
					  vo.setAssignToId(activity.getAssignTo().getId());
				  }
				  //vo.setProject(activity.getLead().getProjects())
				  list.add(vo);
			  }
		  }
		  else if(type.equals(ApplicationConstant.SalesCalenderType.Task))
		  {
			  List<Task> task = this.calenderRepo.showCalenderTaskGrid(assignToId, pfrom, pto);
			  for(Task taskList :task)
			  {
				  SalesCalendarVo vo=new SalesCalendarVo();
				  vo.setDueDateString(DateUtil.dateToString(taskList.getActualStartDT()));
				  vo.setName(taskList.getTaskDetail());
				  if(taskList.getProjectId() != null){
					      if(taskList.getProjectId().getCustomer()!=null){
					  	  vo.setCompany(taskList.getProjectId().getCustomer().getCompany().getName());
					  	  vo.setCompanyId(taskList.getProjectId().getCustomer().getCompany().getId());
					      }
				  }
				   if(taskList.getAssignTo()!=null)
					  {
						  vo.setAssignTo(taskList.getAssignTo().getFirstName());
						  vo.setAssignToId(taskList.getAssignTo().getId());
					  }
				   if(taskList.getProjectId() != null){
					   	  vo.setProject(taskList.getProjectId().getProjectName());
					   	  vo.setProjectId(taskList.getProjectId().getId());
				   }
					  list.add(vo);
			  }
		  }
		  
		  else if(type.equals(ApplicationConstant.SalesCalenderType.Issue))
		  {
			  List<Issue> issue = this.calenderRepo.showCalenderIssueGrid(assignToId, pfrom, pto);
			  for(Issue issueList :issue)
			  {
				  SalesCalendarVo vo=new SalesCalendarVo();
				  vo.setDueDateString(DateUtil.dateToString(issueList.getTargetDate()));
				  vo.setName(issueList.getIssueName());
				   if(issueList.getAssignedTo()!=null)
					  {
						  vo.setAssignTo(issueList.getAssignedTo().getFirstName());
						  vo.setAssignToId(issueList.getAssignedTo().getId());
					  }
				   if(issueList.getProject() != null){
					   	  vo.setProject(issueList.getProject().getProjectName());
					   	  vo.setProjectId(issueList.getProject().getId());
				   }
				   if(issueList.getProject()!= null){
					   if(issueList.getProject().getCustomer()!=null){
					     vo.setCompany(issueList.getProject().getCustomer().getCompany().getName());
					     vo.setCompanyId(issueList.getProject().getCustomer().getCompany().getId());
					   }
				   }
					  list.add(vo);
			  }
		  }
		
		return list;
	}
	
	public List<SetupsValue> getTypes()
	{
		List<SetupsValue> list=new ArrayList<SetupsValue>();
		for(ApplicationConstant.SalesCalenderType type:ApplicationConstant.SalesCalenderType.values())
		{
			list.add(new SetupsValue(Long.valueOf(type.ordinal()),type.name()));
		}
		return list;
	}
	
	public List<SetupsValue> getAssignTo(ApplicationConstant.SalesCalenderType type)
	{
		List<Employee> list=this.calenderRepo.getAssignTo(type);
		
		List<SetupsValue> dList=new ArrayList<SetupsValue>();
		for(Employee employee:list)
		{
			
				dList.add(new SetupsValue(employee.getId(),employee.getFirstName()));
		}
		
		return dList;
	}

}
